	package com.ebeijia.web.controller;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.chanjar.weixin.mp.api.WxMpService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ebeijia.mybatis.pojo.ApplyDetailInfo;
import com.ebeijia.mybatis.pojo.ApplyInfo;
import com.ebeijia.mybatis.pojo.HomeSourceInfo;
import com.ebeijia.service.user.ApplyDetailInfoService;
import com.ebeijia.service.user.HomeService;
import com.ebeijia.util.CheckIdCardUtil;
import com.ebeijia.util.PropertiesUtils;
import com.ebeijia.web.ResponseMessage;

@Controller
@RequestMapping("/applyDetail")
public class ApplyDetailController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ApplyDetailInfoService applyDetailService;
	
	@Autowired
	private WxMpService wxMpService;
	
	@Autowired
	private HomeService homeService;

	/**
	 * 用水申请
	 * 
	 * @param applyDetail
	 * @param request
	 * @param response
	 */
	@RequestMapping("/submitApplyDetailInfo")
	@ResponseBody
	public ResponseMessage insert(HttpServletRequest request, HttpServletResponse response) {
		
		String userName=request.getParameter("userName");
		String phone=request.getParameter("mobile");
		String identity = request.getParameter("identity");
		String applyUserId = request.getParameter("applyUserId");
		String serverIds = request.getParameter("serverIds");
		String homeUserNo = request.getParameter("homeUserNo");
		String applyType = request.getParameter("applyType");
		String userAddress = request.getParameter("userAddress");
		
		//只有当applyType为01（申请用水时才会验证身份证）
		if(applyType != null && "01".equals(applyType)){
			String errors = CheckIdCardUtil.checkIdCard(identity);
			if (null != errors) {
				// 验证身份证信息
				return ResponseMessage.error("0003", errors);
			} 
		}
		ApplyInfo applyInfo = new ApplyInfo();
		ApplyDetailInfo applyDetail = new ApplyDetailInfo();
		
		//自动生成唯一编号 
		int r1=(int)(Math.random()*(10));//产生2个0-9的随机数
		int r2=(int)(Math.random()*(10));
		long now = System.currentTimeMillis();//一个13位的时间戳
		String paymentID =String.valueOf(r1)+String.valueOf(r2)+String.valueOf(now);
		
		applyInfo.setApplyNo(paymentID);
		
		applyInfo.setApplyUserId(applyUserId);
		
		applyInfo.setApplyDate(new Date());
		
		applyInfo.setApplyType(applyType);
		
		applyInfo.setApplyStatus("01");

		applyDetail.setIdNo(identity);
		// 申请ID
		applyDetail.setApplyNo(paymentID);
		//申请号码
		applyDetail.setMobile(phone);
		applyDetail.setUserName(userName);
		//图片地址 
		applyDetail.setIdImagePath("");
		
		applyDetail.setUserAddress(userAddress);
		
		String imgPaths= "";
		if("01".equals(applyType)){
			//调用图片下载
			String path = PropertiesUtils.getProperties("img_load_path");
			String[] serverId = serverIds.split("\\|");
			int i =1;
			for(String ser : serverId){
				try {
					File file = wxMpService.mediaDownload(ser);
					String imgPaht = applyDetailService.copyFile(file, path, paymentID + (i++));
					imgPaths += imgPaht + "|";
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
				} 
			}
			applyDetail.setIdImagePath(imgPaths.substring(0, imgPaths.length() - 1));
		}
		
		if(!"01".equals(applyType)){
			HomeSourceInfo homeInfo = homeService.getHomeUserNo(homeUserNo,applyUserId);
			//当前房源编号
			applyDetail.setHsId(homeInfo.getId());
		}
		
		int count = applyDetailService.insertApplyInfo(applyInfo, applyDetail);
		if (count != 0) {
			return ResponseMessage.error("0000", "申请成功");
		} else {
			return ResponseMessage.error("0001", "申请失败");
		}
		
	}

}
